package com.yl.chainStore.services.impl

import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.yl.chainStore.common.Constants.Name.Companion.ID
import com.yl.chainStore.common.Constants.Name.Companion.STATUS
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.entity.warehouse.TechnologyProcessing
import com.yl.chainStore.entity.warehouse.TechnologyProcessingDetail
import com.yl.chainStore.mapper.warehouse.TechnologyProcessingDetailMapper
import com.yl.chainStore.mapper.warehouse.TechnologyProcessingMapper
import com.yl.chainStore.services.warehouse.IWarehouseService
import com.yl.chainStore.services.pay.IPayCustomerOrderService
import com.yl.chainStore.services.ITechnologyProcessingService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @description :技加工记录
 * @author :吴枫
 * @create :2021-06-22 10:45:00
 */
@Service
class ITechnologyProcessingServiceImpl(
        private val technologyProcessingDetailMapper: TechnologyProcessingDetailMapper,
        private val technologyProcessingMapper: TechnologyProcessingMapper
) : ITechnologyProcessingService {


    @Autowired
    lateinit var payCustomerOrderServices: IPayCustomerOrderService


    companion object {
        private val log = LoggerFactory.getLogger(ITechnologyProcessingService::class.java)
    }


    override fun processDelete(id: Long): ResponseBody {
        log.info("技加工删除id", id)
        technologyProcessingMapper.selectById(id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "没有这条技加工记录")

        technologyProcessingMapper.update(null, Wrappers.update<TechnologyProcessing>().set("enabled", 0).eq("id", id))
        return returnSuccess()
    }


    override fun processUpdate(process: TechnologyProcessing): ResponseBody {
        log.info("技加工修改：process：{}", process)
        val selectById = technologyProcessingMapper.selectById(process.id)
        process.planTime = selectById.planTime

        technologyProcessingMapper.updateById(process)

        for (i in process.details!!) {
            technologyProcessingDetailMapper.updateById(i)
        }
        return returnSuccess()
    }

    override fun processStatusUpdate(data: JSONObject): ResponseBody {
        // TODO: 2021/10/20 要重新做
        log.info("技加工状态修改：data：{}", data)
        //进行状态的判断,是否可以跳到指定状态
        val statusRight = statusRight(data.getLong(ID), data.getString(STATUS))
        if(statusRight.bodyIsError()){
            return statusRight
        }

        //修改状态
        technologyProcessingMapper.update(null, UpdateWrapper<TechnologyProcessing>()
                .eq(ID, data.getLong(ID))
                .set("status", Status.getEnum(data.getString("status"))))
        // 如果修改为已完成则调取订单方法提示
        if (data.getString(STATUS) == Status.COMPLETE.show) {
            //查询当前技加工订单下的所有详情id
            val detailProcess = technologyProcessingDetailMapper.detailProcess(data.getLong(ID))
            for (i in detailProcess) {
                //提醒技加工物品已经到货
                payCustomerOrderServices.goodsArrive(i.orderDetailId)
            }
        }

        return returnSuccess()
    }

    override fun processDetail(id: Long): List<TechnologyProcessingDetail> {
        log.info("技加工记录对象：id{}", id)
        return technologyProcessingDetailMapper.detailProcess(id)
    }


    /**
     * 判断状态是否正确
     */
    private fun statusRight(id: Long, status: String): ResponseBody {
        //查询到当前状态
        val processLogList = technologyProcessingMapper.getProcessListById(id)
        //todo 进行状态判断是否能跳转
        return returnSuccess()


    }



}
